layui.use(['util','tree'], function() {
    var tree = layui.tree
        , layer = layui.layer
        , util = layui.util
        //模拟数据
        , data;
    $.ajax({
        url:"/admin/menu/listAll",
        type:"get",
        success:function (result) {
            if(result.code==0){
                var menus=result.data;
                //获取菜单树结构
                data=getData(menus);
                tree.render({
                    elem: '#test12'
                    ,data: data
                    ,showCheckbox: false  //是否显示复选框
                    ,id: 'demoId1'
                    ,isJump: true //是否允许点击节点时弹出新窗口跳转
                    ,edit: ['add', 'update', 'del'] //操作节点的图标
                    ,operate: function(obj){
                        var type = obj.type; //得到操作类型：add、edit、del
                        var data = obj.data; //得到当前节点的数据
                        var elem = obj.elem; //得到当前节点元素
                        var id = data.id; //得到节点索引
                        if(type === 'add'){ //增加节点
                            layer.open({
                                type: 2
                                ,title:"添加菜单信息"
                                ,area: ['500px', '300px']
                                ,content: "../../view/menu/addMenu.html?parentId="+id
                            });
                        } else if(type === 'update'){ //修改节点
                            layer.open({
                                type: 2
                                ,title:"修改菜单信息"
                                ,area: ['500px', '300px']
                                ,content: "../../view/menu/editMenu.html?id="+id
                            });
                        } else if(type === 'del'){ //删除节点
                            layer.confirm('是否删除名为['+data.title+']菜单节点？', {
                                btn: ['是', '否']
                            }, function(index, layero){
                                $.ajax({
                                    url:"/admin/menu/delete",
                                    type:"post",
                                    data:{id:id},
                                    success:function (result) {
                                        if(result.code==0){
                                            layer.msg("删除成功！");
                                            setInterval(function () {
                                                window.location.reload();
                                            },500);
                                        }else {
                                            layer.msg(result.message);
                                        }
                                    }
                                });
                            },function () {
                                window.location.reload();
                            });
                            return false;
                        };
                    }
                });
            }else {
                layer.msg(result.message);
            }
        }
    });
    $("#add").click(function () {
        layer.open({
            type: 2
            ,anim:1
            ,title:"添加菜单信息"
            ,area: ['500px', '300px']
            ,content: "../../view/menu/addMenu.html"
        });
    });
});

/**
 * 获取子菜单
 * @param menus
 * @param obj
 */
function getTree(menus,obj) {
    var menus2=menus;
    var chlist=[];
    for (var i=0;i<menus2.length;i++){
        var menu=menus2[i];
        if(menu.parentId==obj.id){
            var menuTree={
                title:menu.name,
                id:menu.id,
                spread:true
            };
            getTree(menus,menuTree);
            chlist.push(menuTree);
        }
    }
    obj.children=chlist;
}

/**
 * 获得菜单树数据
 * @param menus
 * @returns {Array}
 */
function getData(menus) {
    var lastMenus=[];
    for (var i=0;i<menus.length;i++){
        var menu=menus[i];
        if(menu.parentId==null){
            var menuTree={
                title:menu.name,
                id:menu.id,
                spread:true
            }
            lastMenus.push(menuTree);
        }
    }
    for (var i=0;i<lastMenus.length;i++){
        getTree(menus,lastMenus[i]);
    }
    return lastMenus;
}